package pATT.DataSetsGenerator.dataSetsGenerator.generator.visual;

import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import pATT.DataSetsGenerator.dataSetsGenerator.attribute.implement.Attribute;
import pATT.DataSetsGenerator.dataSetsGenerator.core.implement.Relation;
import pATT.DataSetsGenerator.dataSetsGenerator.core.implement.RelationManager;
import pATT.DataSetsGenerator.dataSetsGenerator.core.implement.Support;
import pATT.DataSetsGenerator.dataSetsGenerator.data.implement.DataManager;
import pATT.DataSetsGenerator.dataSetsGenerator.data.implement.IData;
import pATT.DataSetsGenerator.dataSetsGenerator.data.implement.RandomWeightData;
import pATT.help.visual.HelpFrame;
import pATT.logger.Message;

@SuppressWarnings("serial")
public class RandomWithWeightFrame extends RandomFrame {
	
	private JPanel jWeightPanel1 = null;  //  @jve:decl-index=0:visual-constraint="64,-13"
	private JLabel jLabel7 = null;
	private JLabel jLabel8 = null;
	private JLabel jLabel9 = null;
	private JTextField jTextField3 = null;
	private JTextField jTextField4 = null;
	private JButton jButton = null;
	public RandomWithWeightFrame() throws HeadlessException {
		super();
		nameGenerator = "Random With Weight Generator";
		Relation relation = RelationManager.instance.getCurrentRelation();
		Vector<IData> vec = new Vector<IData>();
		vec = generateData(relation.getAtributes());
		manager = new DataManager(relation,vec,nameGenerator);
		
		super.setTitle("Relation Generator Rule - "+nameGenerator);
		super.jPanel.add(getJPanel1(), null);
		Dimension pantalla = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension freim = this.getSize();
		if (pantalla.width < freim.width){
			freim.width = pantalla.width;
		}
		if (pantalla.height < freim.height) {
			freim.height = pantalla.height;
		}
		this.setLocation((pantalla.width-freim.width)/2,(pantalla.height-freim.height)/2);

	}
	
	private Vector<IData> generateData(Vector<Attribute> attributes){
		Vector<IData> vector = new Vector<IData>();
		for (int i = 0; i < attributes.size(); i++) {
			vector.addElement(new RandomWeightData(attributes.elementAt(i)));
		}
		return vector;
	}
	
	private JPanel getJPanel1() {
		if (jWeightPanel1 == null) {
			jLabel9 = new JLabel();
			jLabel9.setBounds(new java.awt.Rectangle(236,4,78,22));
			jLabel9.setText("% of examples");
			jLabel9.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			jLabel8 = new JLabel();
			jLabel8.setBounds(new java.awt.Rectangle(140,4,38,22));
			jLabel8.setText("For");
			jLabel8.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			jLabel7 = new JLabel();
			jLabel7.setBounds(new java.awt.Rectangle(21,4,60,22));
			jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			jLabel7.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jLabel7.setText("UseValue");
			jWeightPanel1 = new JPanel();
			jWeightPanel1.setLayout(null);
			jWeightPanel1.setBounds(new java.awt.Rectangle(6,150,341,70));
			jWeightPanel1.setForeground(java.awt.Color.black);
			jWeightPanel1.setBackground(java.awt.SystemColor.activeCaptionBorder);
			
			jWeightPanel1.add(jLabel7, null);
			jWeightPanel1.add(getJTextField3(), null);
			jWeightPanel1.add(jLabel8, null);
			jWeightPanel1.add(getJTextField4(), null);
			jWeightPanel1.add(jLabel9, null);
			jWeightPanel1.add(getJButton(), null);
		}
		return jWeightPanel1;
	}
	
	
	private JTextField getJTextField3() {
		if (jTextField3 == null) {
			jTextField3 = new JTextField();
			jTextField3.setLocation(new java.awt.Point(81,4));
			jTextField3.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jTextField3.setSize(new java.awt.Dimension(60,22));
		}
		return jTextField3;
	}
	
	private JTextField getJTextField4() {
		if (jTextField4 == null) {
			jTextField4 = new JTextField();
			jTextField4.setLocation(new java.awt.Point(177,4));
			jTextField4.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jTextField4.setSize(new java.awt.Dimension(60,22));
		}
		return jTextField4;
	}
	
	private boolean isIntegerCorrect() {
		boolean valid = true;
		if(!Support.isDigit(jTextField4.getText())  || jTextField4.getText().trim().equals("") || jTextField4.getText().trim().equals("<complete>")){
			valid = false;
			jTextField4.setText("<complete>");
			if (jTextField3.getText().trim().equals(""))
				jTextField3.setText("<complete>");
		} 
		return valid;	
	}
	
	private boolean isNominalCorrect() {
		boolean valid = true;
		if(jTextField3.getText().trim().equals("") || jTextField3.getText().trim().equals("<complete>")){
			valid = false;
			jTextField3.setText("<complete>");
			
		}
		return valid;
	}
	
	private boolean isValueCorrect(){
		int pos = jComboBox.getSelectedIndex();
		Attribute attr = manager.getRelation().getAtributes().get(pos);
		if (!attr.getValues().contains(jTextField3.getText())){
			jTextField3.setText("<complete>");
			return false;
		}		
		return true;
		
	}

	private boolean isPorcentCorrect(String po){
		int porcent=Integer.valueOf(po);
		if (!((porcent>=0) && (porcent<=100))){
			jTextField4.setText("<complete>");
			return false;
		}
		return true;
	}
	
	
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new java.awt.Rectangle(241,34,86,21));
			jButton.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jButton.setBackground(java.awt.SystemColor.activeCaptionBorder);
			jButton.setText("Set weight");
			jButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if(jComboBox.getSelectedIndex() != -1)
					if ((isIntegerCorrect() && isNominalCorrect()) &&  isValueCorrect() && isPorcentCorrect(jTextField4.getText())){
						int pos = jComboBox.getSelectedIndex();
						RandomWeightData data = (RandomWeightData)manager.getData(pos);
						String value = jTextField3.getText();
						int weight = Integer.parseInt(jTextField4.getText());
						data.setValueWeight(value);
						data.setWeight(weight);	
						jTextField3.setText("");
						jTextField4.setText("");
					}	
				}
			});
		}
		return jButton;
	}
	protected void callHelp() {
		
		new HelpFrame(Message.TITLE_RANDOM_WEIGHT,Message.BODY_RANDOM_WEIGHT).show();
		
	}

	
}  //  @jve:decl-index=0:visual-constraint="47,12"
